package com.c.ctgproject.ui.sell.parts

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.CarInfoEntity
import com.c.ctgproject.mode.MyConstants
import com.c.ctgproject.utils.*
import kotlinx.android.synthetic.main.activity_seller_sccan.*
import kotlinx.android.synthetic.main.activity_seller_sccan.mTitleBar
import org.jetbrains.anko.startActivity

/**
 *作者:LiBW
 *创建日期: 2020/9/8
 *描述: 销售扫码进入页面
 */
class SellerScanActivity : BaseActivity() {
    private val mViewModel: SellerScanViewModel by lazy { ViewModelProvider(this)[SellerScanViewModel::class.java] }
    private val carCode: String by lazy { intent.getStringExtra(MyConstants.CAR_CODE) }
    private var carId: String = ""

    //有无销售价值 0- 无 1 - 有
    private var hasValue = 0
    override fun getLayout() = R.layout.activity_seller_sccan
    //todo  临时解决办法
    private var clickFlag=false

    override fun event() {
        changeTransparentStatusBar()
        changeStatusIconColor(true)
        loading.show()

        MyLog.d("carCode$carCode")
        //获取车辆详情
        mTitleBar.setTitle(carCode)
        mTitleBar.setTitleTextSize(14f)
        mViewModel.getCarDetail(carCode)

        //获取车辆详情回调
        mViewModel.carDetail.observe(this, Observer {
            loading.dismiss()
            MyLog.d("车辆详情回调:$it")
            val data = initData(it)

            if (data.status == 0) {
                val entity = getGson().fromJson(it, CarInfoEntity::class.java).data
                entity?.let { mData ->
                    mCarBrandTV.text = mData.carBrand.handlerNull()
                    mVINTV.text = mData.carVin.handlerNull()
                    mTimeTV.text = mData.enterTime.handlerNull()
                    carId = mData.carId.handlerNull()
                    val mHasValue = mData.doSellValue
                    if (mHasValue != null) {
                        hasValue = mHasValue
                        if (hasValue==0){
                            mNoBTN.isChecked=true
                        }else{
                            mYesBTN.isChecked=true
                        }
                    }else{
                        clickFlag=true
                    }
                }
            } else {
                CustomToast.showToast(this, data.msg.toString())
            }
        })
        //是否有价值回调
        mViewModel.preserveValue.observe(this, Observer {
            val data = initData(it)
            if (data.status == 0) {
                if (clickFlag){
                    CustomToast.showToast(this, "保存销售价值成功")
                }
                clickFlag=true
            } else {
                CustomToast.showToast(this, data.msg.toString())
            }
        })

        mViewModel.error.observe(this, Observer {
            loading.dismiss()
            CustomToast.showToast(this, it)
        })
    }

    override fun click() {
        //是否有价值 选择按钮
        mHasValueRG.setOnCheckedChangeListener { _, checkedId ->
            if (checkedId == R.id.mYesBTN) {
                hasValue = 1
            }
            if (checkedId == R.id.mNoBTN) {
                hasValue = 0
            }
            mViewModel.preserveValue(carCode, hasValue)
        }

        //车辆已售出配件
        mSoldPartRL.setOnClickListener {
            startActivity<SingleCarPartsActivity>(MyConstants.CAR_CODE to carCode)
        }
        //配件销售
        mSellPartRL.setOnClickListener {
            startActivity<PartsSellActivity>(MyConstants.CAR_CODE to carCode, MyConstants.CAR_ID to carId)
        }

    }

}